//
//  YFFriendsVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/22.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFFriendsVC.h"
#import "HJFriendsCell.h"
#import "HJSystemMsgVC.h"
#import "HJFriendsListModel.h"
#import "HJNewFriendsVC.h"
#import "HJChatRoomVC.h"

@interface YFFriendsVC ()

@property (nonatomic, strong) NSMutableArray <HJListModel *>* friendList;
//@property (nonatomic, strong) HJFriendsListModel * sectionModel;
//@property (nonatomic, strong) NSMutableDictionary * friendDic;
//@property (nonatomic, strong) UILabel * footerLabel;
@end

static NSString *const identifier = @"FriendsCell";
@implementation YFFriendsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.friendDic = [NSMutableDictionary dictionary];
    [self loadNetworkData];
    [mNotificationCenter addObserver:self selector:@selector(refreshDataWithHeader) name:@"agreeNewFriendNotification" object:nil];
}

-(void)configTableView {
    self.needHeaderRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor colorWithHex:0xf2f2f2];
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 20, 0);
//    self.tableView.sectionIndexColor = [UIColor colorWithHex:0x666666];
//    self.tableView.tableFooterView = [self configFooter];
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

-(NSMutableArray<HJListModel *> *)friendList {
    if (!_friendList) {
        _friendList = [NSMutableArray array];
    }
    return _friendList;
}
-(void)loadNetworkData {
    NSString * url = [Api stringByAppendingString:FiendListApi];
    [HJHttpTool post:url params:@{@"userId":[HJUserManager getUserModel].userId} success:^(HJResponeds *result) {
//        self.sectionModel = [[HJFriendsListModel alloc]init];
        [self.friendList removeAllObjects];
//        self.sectionModel = [HJFriendsListModel yy_modelWithDictionary:result.data];
        NSArray * arr = [NSArray yy_modelArrayWithClass:[HJListModel class] json:result.data];
        [self.friendList addObjectsFromArray:arr];
//        NSMutableArray * dicArr = [NSMutableArray array];
//        NSMutableDictionary * allDic = [NSMutableDictionary dictionary];
//        for (HJListModel * listModel in arr) {
//            NSString * firstLetter = [CommonTools getFirstLetterFromString:listModel.nickName];
//            if ([[allDic allKeys] containsObject:firstLetter]) {// 包含
//                dicArr = [allDic objectForKey:firstLetter];
//                [dicArr addObject:listModel];
//                [allDic setObject:dicArr forKey:firstLetter];
//            }else {// 不包含
//                dicArr = [[NSMutableArray alloc]initWithObjects:listModel, nil];
//                [allDic setObject:dicArr forKey:firstLetter];
//            }
//        }
//        self.footerLabel.text = [NSString stringWithFormat:@"%zd位联系人",allDic.count];
//        self.friendDic = allDic;
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(id result) {
        [self.tableView endRefresh];
        [self showErrorHUDWithError:result];
    }];
}


//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return [self.friendDic allKeys].count+1;
//}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (section==0) return self.systemMsg.count;
//    return [[self.friendDic allValues][section-1] count];
    return self.friendList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJFriendsCell * friendsCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!friendsCell) {
        friendsCell = [[HJFriendsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
//    if (indexPath.section==0) {
//        NSDictionary * dic = self.systemMsg[indexPath.row];
//        if (indexPath.row==0) {
//            friendsCell.promptLabel.hidden = !self.sectionModel.friendNoticeAmount.boolValue;
//            friendsCell.promptLabel.text = self.sectionModel.friendNoticeAmount;
//        }else {
//            friendsCell.promptLabel.hidden = !self.sectionModel.systemNoticeAmount.boolValue;
//            friendsCell.promptLabel.text = self.sectionModel.systemNoticeAmount;
//        }
//        tableView.separatorInset = UIEdgeInsetsMake(0, 62, 0, 0);
//        friendsCell.name.text = dic[@"title"];
//        friendsCell.headImage.image = setImage(dic[@"image"]);
//    }else {
        friendsCell.promptLabel.hidden = YES;
        tableView.separatorInset = UIEdgeInsetsMake(0, 43, 0, 0);
        [friendsCell loadFriendsData:self.friendList[indexPath.row]];
//    }
    return friendsCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (section==0) return 0.01;
//    return 20;
//}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    if (section==0) return [UIView new];
//    UIView * titleHead = [UIView new];
//    titleHead.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
//    titleHead.frame = CGRectMake(0, 0, tableView.width, 20);
//
//    UILabel * label = [UILabel new];
//    label.text = [self.friendDic allKeys][section-1];
//    label.textColor = [UIColor colorWithHex:0x999999];
//    label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
//    label.frame = CGRectMake(12, 0, titleHead.width-12, titleHead.height);
//    label.backgroundColor = [UIColor clearColor];
//    [titleHead addSubview:label];
//    return titleHead;
//}

//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
////    if (indexPath.section==0) return NO;
//    return YES;
//}

//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
////    if (indexPath.section==0) return UITableViewCellEditingStyleNone;
//    return UITableViewCellEditingStyleDelete;
//}
//
//-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewRowAction * deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        NSString * url = [Api stringByAppendingString:DeleteFriendApi];
////        HJListModel * model = [self.friendDic allValues][indexPath.section-1][indexPath.row];
//        NSMutableDictionary * params = [NSMutableDictionary dictionary];
//        params[@"friendUserId"] = self.friendList[indexPath.row].userId;
//        params[@"userId"] = [HJUserManager getUserModel].userId;
//        WS(weakSelf)
//        [HJHttpTool post:url params:params success:^(HJResponeds *result) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [weakSelf showSuccessHUDWithStr:@"删除成功"];
////                [[self.friendDic allValues][indexPath.section-1] removeObject: model];
//                [self.friendList removeObject:self.friendList[indexPath.row]];
//                NSIndexPath * index = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
//                [tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
////                if ([self.friendDic allValues][indexPath.section-1]==nil) {
////                    [self.friendDic removeObjectForKey:[self.friendDic allKeys][indexPath.section-1]];
////                }
////                [tableView reloadData];
//            });
//        } failure:^(id result) {
//            [weakSelf showErrorHUDWithError:result];
//        }];
//    }];
//    deleteAction.backgroundColor = [UIColor colorWithPatternImage:setImage(@"friendsChat")];
//    return @[deleteAction];
//}


//-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    return [[[self friendDic] allKeys] sortedArrayUsingSelector:@selector(compare:)];
//}
//
//- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
//    return index;
//}

//-(UIView *)configFooter {
//    UIView * titlefooter = [UIView new];
//    titlefooter.backgroundColor = [UIColor clearColor];
//    titlefooter.frame = CGRectMake(0, 0, self.tableView.width, 30);
//
//    UILabel * label = [UILabel new];
//    label.textColor = [UIColor colorWithHex:0xC2C2C2];
//    label.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14];
//    label.frame = CGRectMake(0, 18, titlefooter.width, 14);
//    label.textAlignment = NSTextAlignmentCenter;
//    self.footerLabel = label;
//    [titlefooter addSubview:label];
//    return titlefooter;
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    switch (indexPath.section) {
//        case 0:
//        {
//            if (indexPath.row==0) {
//                self.sectionModel.friendNoticeAmount = @"0";
//                HJNewFriendsVC * newFriendVC = [HJNewFriendsVC new];
//                [self.navigationController pushViewController:newFriendVC animated:YES];
//                [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
//            }else {
//                self.sectionModel.systemNoticeAmount = @"0";
//                HJSystemMsgVC * systemVC =[[HJSystemMsgVC alloc]init];
//                [self.navigationController pushViewController:systemVC animated:YES];
//                [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
//            }
//        }
//            break;
//        default:
//        {
            HJChatRoomVC * chatRoomVC = [[UIStoryboard storyboardWithName:@"Chat" bundle:nil]instantiateViewControllerWithIdentifier:chatRoom];
//            HJListModel * listModel = [self.friendDic allValues][indexPath.section-1][indexPath.row];
            RCConversationModel * conversationModel = [[RCConversationModel alloc]init];
            conversationModel.conversationTitle = self.friendList[indexPath.row].nickName;
    
            conversationModel.conversationType = ConversationType_PRIVATE;
            conversationModel.targetId = self.friendList[indexPath.row].userId;
            chatRoomVC.conversationModel = conversationModel;
            chatRoomVC.avatarUrl = self.friendList[indexPath.row].avatar;
            [self.navigationController pushViewController:chatRoomVC animated:YES];
//        }
//            break;
//    }
}
@end
